package com.sky.service.admin.impl;

import com.sky.mapper.admin.WorkSpaceMapper;
import com.sky.service.admin.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderDayAmountVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private WorkSpaceMapper workSpaceMapper;
    /**
     * 营业数据
     * @return
     */
    @Override
    public BusinessDataVO businessData() {
        log.info("营业数据查询");
        LocalDate now = LocalDate.now();
        //获取营业额数据
        List<OrderDayAmountVO> orderDayAmountVOS =workSpaceMapper.getOrderAmount(now.toString(), now.plusDays(1).toString());
        //判断集合是否为空
        if (CollectionUtils.isEmpty(orderDayAmountVOS)){
            return new BusinessDataVO(
                    0.0,
                    0,
                    0.0,
                    0.0,
                    0
            );
        }
        //获取当天的营业额
        OrderDayAmountVO orderDayAmountVO = orderDayAmountVOS.get(0);
        //补充数据
        Integer completeNum = orderDayAmountVO.getCompleteNum();
        Integer totalNum = orderDayAmountVO.getTotalNum();
        //平均客单价
        double price=0;
        if (completeNum!=0){
            price=orderDayAmountVO.getAmount().doubleValue()/completeNum;
        }
        //订单完成率
        double rate=completeNum*1d/totalNum;
        //封装新增用户数
        Integer newUsers=workSpaceMapper.getNewUserCount(now);
        BusinessDataVO businessDataVO=new BusinessDataVO().builder()
                .turnover(orderDayAmountVO.getAmount().doubleValue())
                .validOrderCount(completeNum)
                .orderCompletionRate(rate)
                .unitPrice(price)
                .newUsers(newUsers).build();
        return businessDataVO;
    }

    /**
     * 菜品总览
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        log.info("菜品总览");
        DishOverViewVO dishOverViewVO=workSpaceMapper.overviewDishes();
        return dishOverViewVO;
    }

    /**
     * 套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        SetmealOverViewVO setmealOverViewVO=workSpaceMapper.overviewSetmeals();
        return setmealOverViewVO;
    }
}
